package com.example.minademo.mina;

import org.apache.mina.core.session.IoSession;

import java.util.concurrent.ConcurrentHashMap;

public class MinaServiceManager {

    private ConcurrentHashMap<Long,IoSession>  mSessions = new ConcurrentHashMap<>();
    private static MinaServiceManager mInstance = null;
    public static MinaServiceManager getInstance(){
        if(mInstance == null){
            synchronized (MinaServiceManager.class){
                if(mInstance == null){
                    mInstance = new MinaServiceManager();
                }
            }
        }
        return mInstance;
    }

    public IoSession getSessionById(Long id) {
        return mSessions.get(id);
    }

    public void addSession(IoSession mSession) {
        if(mSessions.get(mSession.getId()) == null){
            mSessions.put(mSession.getId(),mSession);
        }
    }


    public void sendToClient(Long id , Object msg){
        IoSession session = mSessions.get(id);
        if(session != null){
            session.write(msg);
        }
    }

}
